package com.example.onlinetaxi.view.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.example.onlinetaxi.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

// 自定义时间选择对话框
public class TimePickerDialog extends Dialog {
    /* Constructor */
    private TimePickerDialog(Context context) {
        super(context);
    }

    private TimePickerDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private TimePickerDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static void showTimePickerDialog(Context context, View.OnClickListener cancelOnClick,
                                            View.OnClickListener confirmOnClick) {
        TimePickerDialog.Builder builder = new TimePickerDialog.Builder(context);
        builder.setTitle("叫车时间选择");
        builder.setButtonCancel("现在叫车", cancelOnClick);
        builder.setButtonConfirm("预约叫车", confirmOnClick);

        TimePickerDialog customDialog = builder.create();
        customDialog.show();
    }

    /* Builder */
    public static class Builder {
        // 日历对象
        Calendar calendar = Calendar.getInstance();
        // 日期选择器控件
        public static NumberPicker yearPicker;
        public static NumberPicker monthPicker;
        public static NumberPicker datePicker;
        public static NumberPicker hourPicker;
        public static NumberPicker minutePicker;

        // 控件
        private TextView tvTitle;
        private Button btnCancel;
        private Button btnConfirm;

        private View mLayout;
        private View.OnClickListener mButtonCancelClickListener;
        private View.OnClickListener mButtonConfirmClickListener;

        private TimePickerDialog mDialog;

        public Builder(Context context) {
            mDialog = new TimePickerDialog(context, R.style.custom_dialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 加载布局文件
            mLayout = inflater.inflate(R.layout.time_picker_dialog, null, false);
            // 添加布局文件到 Dialog
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            yearPicker = mLayout.findViewById(R.id.number_picker_year);
            monthPicker = mLayout.findViewById(R.id.number_picker_month);
            datePicker = mLayout.findViewById(R.id.number_picker_date);
            hourPicker = mLayout.findViewById(R.id.number_picker_hour);
            minutePicker = mLayout.findViewById(R.id.number_picker_minute);

            tvTitle = mLayout.findViewById(R.id.time_picker_title);
            btnCancel = mLayout.findViewById(R.id.btn_cancel);
            btnConfirm = mLayout.findViewById(R.id.btn_confirm);

            setNumberLimit();
        }

        private void setNumberLimit() {
            //限制年份范围为前后五年
            int yearNow = calendar.get(Calendar.YEAR);
            yearPicker.setMinValue(yearNow);
            yearPicker.setMaxValue(yearNow + 1);
            yearPicker.setValue(yearNow);
            yearPicker.setWrapSelectorWheel(false);  //关闭选择器循环

            //设置月份范围为1~12
            monthPicker.setMinValue(1);
            monthPicker.setMaxValue(12);
            monthPicker.setValue(calendar.get(Calendar.MONTH) + 1);
            monthPicker.setWrapSelectorWheel(false);

            //日期限制存在变化，需要根据当月最大天数来调整
            datePicker.setMinValue(1);
            datePicker.setMaxValue(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            datePicker.setValue(calendar.get(Calendar.DATE));
            datePicker.setWrapSelectorWheel(false);

            //24小时制，限制小时数为0~23
            hourPicker.setMinValue(0);
            hourPicker.setMaxValue(23);
            hourPicker.setValue(calendar.get(Calendar.HOUR_OF_DAY));
            hourPicker.setWrapSelectorWheel(false);

            //限制分钟数为0~59
            minutePicker.setMinValue(0);
            minutePicker.setMaxValue(59);
            minutePicker.setValue(calendar.get(Calendar.MINUTE));
            minutePicker.setWrapSelectorWheel(false);

            //为年份和月份设置监听
            yearPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                    String dateStr = String.format(Locale.CHINA, "%d-%d", yearPicker.getValue(), monthPicker.getValue());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM", Locale.CHINA);
                    Calendar calendar = Calendar.getInstance();
                    try {
                        calendar.setTime(simpleDateFormat.parse(dateStr));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    int dateValue = datePicker.getValue();
                    int maxValue = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                    datePicker.setMaxValue(maxValue);
                    //重设日期值，防止月份变动时超过最大值
                    datePicker.setValue(Math.min(dateValue, maxValue));
                }
            });
            monthPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                    String dateStr = String.format(Locale.CHINA, "%d-%d", yearPicker.getValue(), monthPicker.getValue());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM", Locale.CHINA);
                    Calendar calendar = Calendar.getInstance();
                    try {
                        calendar.setTime(simpleDateFormat.parse(dateStr));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    int dateValue = datePicker.getValue();
                    int maxValue = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                    datePicker.setMaxValue(maxValue);
                    //重设日期值，防止月份变动时超过最大值
                    datePicker.setValue(Math.min(dateValue, maxValue));
                }
            });
        }

        // 设置标题
        public Builder setTitle(String title) {
            tvTitle.setText(title);
            tvTitle.setVisibility(View.VISIBLE);
            return this;
        }

        // 设置取消按钮文字和监听
        public Builder setButtonCancel(String text, View.OnClickListener listener) {
            btnCancel.setText(text);
            mButtonCancelClickListener = listener;
            return this;
        }

        // 设置确认按钮文字和监听
        public Builder setButtonConfirm(String text, View.OnClickListener listener) {
            btnConfirm.setText(text);
            mButtonConfirmClickListener = listener;
            return this;
        }

        // 创建对话框
        public TimePickerDialog create() {
            btnCancel.setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    mButtonCancelClickListener.onClick(view);
                }
            });

            btnConfirm.setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    mButtonConfirmClickListener.onClick(view);
                }
            });

            mDialog.setContentView(mLayout);
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(false);
            return mDialog;
        }
    }
}
